<template>
  <el-aside :width="asideWidth">
    <div class="logo-container" :class="{ collapsed: isCollapse }" @click="navigateToHome">
      <img src="@/assets/img/vue.svg" alt="应用logo" class="logo-image" />
      <h4 v-show="!isCollapse" class="logo-title">数字邮管</h4>
    </div>

    <el-menu
      class="vertical-menu"
      :class="{ 'collapsed-menu': isCollapse }"
      router
      :default-active="activePath"
      :collapse="isCollapse"
      :collapse-transition="false"
    >
      <template v-for="(menuItem, index) in menuList" :key="`menu-${index}`">
        <!-- 单层菜单项 -->
        <el-menu-item
          v-if="!menuItem.children"
          :index="menuItem.path"
          @click="handleMenuSelect(menuItem)"
        >
          <component :is="menuItem.icon" class="menu-icon" />
          <template #title>
            <span class="menu-label">{{ menuItem.label }}</span>
          </template>
        </el-menu-item>

        <!-- 嵌套子菜单 -->
        <el-sub-menu v-else :index="menuItem.name">
          <template #title>
            <component :is="menuItem.icon" class="menu-icon" />
            <span v-show="!isCollapse" class="menu-label">{{ menuItem.label }}</span>
          </template>
          <el-menu-item
            v-for="childItem in menuItem.children"
            :key="childItem.path"
            :index="childItem.path"
            @click="handleMenuSelect(childItem, menuItem)"
          >
            <component :is="childItem.icon" class="menu-icon" />
            <span class="menu-label">{{ childItem.label }}</span>
          </el-menu-item>
        </el-sub-menu>
      </template>
    </el-menu>
  </el-aside>
</template>

<script>
import { computed } from 'vue'
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";

export default {
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()

    // 从路由配置生成菜单列表（示例数据，需替换为实际路由配置）
    const menuList = computed(() => router.options.routes.find(r => r.path === '/')?.children || [])

    // 计算属性
    const isCollapse = computed(() => store.state.isCollapse)
    const asideWidth = computed(() => isCollapse.value ? '64px' : '250px')
    const activePath = computed(() => route.path)

    /**
     * 处理菜单项点击事件
     * @param {Object} menuItem - 当前点击的菜单项
     * @param {Object|null} parentItem - 父级菜单项（针对子菜单）
     */
    const handleMenuSelect = (menuItem, parentItem = null) => {
  store.commit('setActiveMenu', {
    val: menuItem,
    parentMenus: getParentHierarchy(menuItem, parentItem)
  })
}

    /**
     * 获取菜单项的父级层级结构
     * @returns {Array} 父级菜单项数组
     */
    const getParentHierarchy = (currentItem, parentItem) => {
      const hierarchy = []
      if (parentItem) {
        hierarchy.push({
          path: parentItem.path,
          label: parentItem.label
        })
      }
      return hierarchy
    }

    return {
      menuList,
      isCollapse,
      asideWidth,
      activePath,
      handleMenuSelect,
      navigateToHome: () => store.dispatch('navigateToHome')
    }
  }
}
</script>

<style scoped lang="less">
.el-aside {
  transition: width 0.3s ease;
  background: var(--el-menu-bg-color);

  .logo-container {
    display: flex;
    align-items: center;
    height: 60px;
    padding: 0 20px;
    cursor: pointer;
    transition: all 0.3s ease;

    &.collapsed {
      justify-content: center;
      padding: 0;
    }

    .logo-image {
      width: 28px;
      height: 28px;
      margin-right: 12px;
    }

    .logo-title {
      margin: 0;
      font-size: 18px;
      color: var(--el-color-primary);
    }
  }

  .vertical-menu {
    border-right: none;
    transition: all 0.3s ease;

    &:not(.el-menu--collapse) {
      width: 250px;
    }

    &.collapsed-menu {
      width: 64px;

      .menu-label {
        display: none;
      }

      .el-sub-menu__icon-arrow {
        display: none;
      }
    }

    .menu-icon {
      width: 20px;
      height: 20px;
      margin-right: 12px;
    }

    .menu-label {
      font-size: 14px;
    }

    .el-menu-item,
    .el-sub-menu__title {
      &:hover {
        color: var(--el-color-primary);
        background-color: var(--el-menu-hover-bg-color);
      }

      &.is-active {
        color: #fff;
        background-color: var(--el-color-primary);
      }
    }
  }
}
</style>